/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tchepannou.rails.core.api;

import com.tchepannou.rails.core.annotation.Job;
import com.tchepannou.rails.core.util.I18nThreadLocal;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import javax.jms.JMSException;
import javax.mail.MessagingException;

/**
 * <code>JobController</code> contains jobs (methods) that are executed periodically
 * in background. Job methods are defined by the annotation {@link Job}, with a cron-expression that
 * defines their schedule of execution.
 *
 * @author herve
 */
public class JobController
    implements Controller
{
    //-- Static Attributes
    public static final String CLASSNAME_SUFFIX = "JobController";

    //-- Attributes
    private JobContext _jobContext;

    //-- Public methods
    public void setJobContext (JobContext context)
    {
        _jobContext =  context;
    }

    public JobContext getJobContext ()
    {
        return _jobContext;
    }

    public I18n getI18n ()
    {
        return I18nThreadLocal.getI18n ();
    }


    //-- Protected methods
    /**
     * Deliver an email
     *
     * @param template Name of the email template. The email is located at <code>/mail/<i>mailer.name()</i>/<i>template</i></code>
     * @param data  Data to apply to the template
     * @param mailer Email controller
     *
     * @throws IOException if any error while loading the email template
     * @throws MessagingException if any error while sending the email
     */
    protected void deliver (String template, Map data, MailController mailer)
        throws IOException, MessagingException
    {
        getJobContext ().getContainerContext ().deliver (template, data, mailer);
    }


    /**
     * Send a message to a JMS destination with no data
     *
     * @see ContainerContext#sendMessage(java.lang.String, java.io.Serializable)
     */
    protected void sendMessage (String destination)
        throws JMSException
    {
        sendMessage (destination, null);
    }


    /**
     * @see ContainerContext#sendMessage(java.lang.String, java.io.Serializable)
     */
    protected void sendMessage (String destination, Serializable message)
        throws JMSException
    {
        getJobContext ().getContainerContext ().sendMessage (destination, message);
    }

}
